import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import axios from 'axios'

export const useCarStore = defineStore('car', () => {
  // 状态
  const isConnected = ref(false)
  const connectionStatus = ref('disconnected') // disconnected, connecting, connected
  const currentSpeed = ref({ x: 0, y: 0 })
  const wheelSpeeds = ref({ l1: 0, l2: 0, r1: 0, r2: 0 })
  const isRecording = ref(false)
  const isTracking = ref(false)
  const networkConfig = ref({
    ip: '192.168.1.11',
    tcpPort: 6000,
    videoPort: 6500
  })

  // 计算属性
  const statusText = computed(() => {
    switch (connectionStatus.value) {
      case 'connected':
        return '已连接'
      case 'connecting':
        return '连接中...'
      case 'disconnected':
        return '未连接'
      default:
        return '未知状态'
    }
  })

  const statusClass = computed(() => {
    switch (connectionStatus.value) {
      case 'connected':
        return 'status-connected'
      case 'connecting':
        return 'status-connecting'
      case 'disconnected':
        return 'status-disconnected'
      default:
        return 'status-disconnected'
    }
  })

  // 网络配置
  const configNetwork = async (config) => {
    try {
      connectionStatus.value = 'connecting'
      const response = await axios.post('/api/network/config', config)
      if (response.data.code === 200) {
        isConnected.value = true
        connectionStatus.value = 'connected'
        networkConfig.value = config
        return { success: true, message: '网络配置成功' }
      } else {
        connectionStatus.value = 'disconnected'
        return { success: false, message: response.data.message }
      }
    } catch (error) {
      connectionStatus.value = 'disconnected'
      return { success: false, message: error.message }
    }
  }

  // 获取连接状态
  const getConnectionStatus = async () => {
    try {
      const response = await axios.get('/api/network/status')
      if (response.data.code === 200) {
        isConnected.value = response.data.data
        connectionStatus.value = isConnected.value ? 'connected' : 'disconnected'
      }
    } catch (error) {
      console.error('获取连接状态失败:', error)
    }
  }

  // 断开连接
  const disconnect = async () => {
    try {
      await axios.post('/api/network/disconnect')
      isConnected.value = false
      connectionStatus.value = 'disconnected'
    } catch (error) {
      console.error('断开连接失败:', error)
    }
  }

  // 按钮控制
  const buttonControl = async (direction) => {
    if (!isConnected.value) {
      return { success: false, message: '未连接到小车' }
    }
    
    try {
      const response = await axios.post('/api/car/control/button', null, {
        params: { direction }
      })
      return { success: response.data.code === 200, message: response.data.message }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 摇杆控制
  const joystickControl = async (speedX, speedY) => {
    if (!isConnected.value) {
      return { success: false, message: '未连接到小车' }
    }
    
    try {
      currentSpeed.value = { x: speedX, y: speedY }
      const response = await axios.post('/api/car/control/joystick', {
        speedX,
        speedY
      })
      return { success: response.data.code === 200, message: response.data.message }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 四轮控制
  const wheelControl = async (l1, l2, r1, r2) => {
    if (!isConnected.value) {
      return { success: false, message: '未连接到小车' }
    }
    
    try {
      wheelSpeeds.value = { l1, l2, r1, r2 }
      const response = await axios.post('/api/car/control/wheels', {
        l1, l2, r1, r2
      })
      return { success: response.data.code === 200, message: response.data.message }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 循迹控制
  const trackingControl = async (start) => {
    if (!isConnected.value) {
      return { success: false, message: '未连接到小车' }
    }
    
    try {
      const endpoint = start ? '/api/car/tracking/start' : '/api/car/tracking/stop'
      const response = await axios.post(endpoint)
      if (response.data.code === 200) {
        isTracking.value = start
      }
      return { success: response.data.code === 200, message: response.data.message }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  // 相机控制
  const cameraControl = async (action) => {
    if (!isConnected.value) {
      return { success: false, message: '未连接到小车' }
    }
    
    try {
      let endpoint = ''
      switch (action) {
        case 'photo':
          endpoint = '/api/car/camera/photo'
          break
        case 'record_start':
          endpoint = '/api/car/camera/record/start'
          break
        case 'record_stop':
          endpoint = '/api/car/camera/record/stop'
          break
        default:
          return { success: false, message: '未知的相机操作' }
      }
      
      const response = await axios.post(endpoint)
      if (response.data.code === 200) {
        if (action === 'record_start') {
          isRecording.value = true
        } else if (action === 'record_stop') {
          isRecording.value = false
        }
      }
      return { success: response.data.code === 200, message: response.data.message }
    } catch (error) {
      return { success: false, message: error.message }
    }
  }

  return {
    // 状态
    isConnected,
    connectionStatus,
    currentSpeed,
    wheelSpeeds,
    isRecording,
    isTracking,
    networkConfig,
    
    // 计算属性
    statusText,
    statusClass,
    
    // 方法
    configNetwork,
    getConnectionStatus,
    disconnect,
    buttonControl,
    joystickControl,
    wheelControl,
    trackingControl,
    cameraControl
  }
}) 